<?php
	session_start();
	require('../Model/mysql.php');
	require('../host_config.php');
	if (!isset($_SESSION['shopid']) && isset($_SESSION['flag'])) {
        echo "<script language='javascript'>";
        echo "alert('You have no permission to read this page!');";
        echo "history.back();";
        echo "</script>";
    	header("location:../login.php");
	}else if($_SEESION['flag'] == "2") {
    }else if($_SESSION['shopid'] != $_GET['id']) {
        header("location:select.php?id=".$_SESSION['shopid']);
    }
		$mysql = new MySQL();
        $link = $mysql->connect($mysql_host, $mysql_user, $mysql_passwd, $mysql_db);
		
		if(isset($_POST['delid']) && !isset($_POST['id'])) {
			$delid = $_POST['delid'];
			$delid = mysql_real_escape_string($delid);
			
			$id = $_GET['id'];
			$id = mysql_real_escape_string($id);
			
			
			$sql = "SELECT `clientid`, `shopid`, `clientname`, `orderofnum`, `cellphone`, `ordertime`, `arrivetime`, `imsi`, `gcm_regid`, `flag` FROM `Client` WHERE `clientid` = '$delid'";
			$row = $mysql->query_row($sql, $link);
			
			
			$clientid = $row[0];
			$shopid = $row[1];
			$clientname = $row[2];
			$orderofumber = $row[3];
			$cellphone = $row[4];
			$ordertime = $row[5];
			$arrivetime = $row[6];
			$imsi = $row[7];
			//$gcm_regid = $row[8];
			$tabletype = $row[9];
			$leavetime = date("Y-m-d H:i");
			
			
			//$sql = "INSERT INTO `Statistics` (`clientid`, `shopid`, `clientname`, `orderofnum`, `cellphone`, `ordertime`, `arrivetime`, `leavetime`, `tabletype`, `imsi`) VALUES ('$clientid', '$shopid', '$clientname', '$orderofumber', '$cellphone','$ordertime', '$arrivetime', '$leavetime', '$tabletype', '$imsi')";
			$sql = "UPDATE DiningRecord SET leavetime = '$leavetime' WHERE staid = '$delid'";
			$mysql->query($sql, $link);
			
			
			$sql = "SELECT `currentnumber` FROM `Shops` WHERE `fullname` = '$ShopName'";
			$row = $mysql->query_row($sql, $link);
			$currentnumber = $row[0];
			
			$currentnumber = $currentnumber - $orderofnumber;
			//$current = ($current) < 0 ? 0 : $current;
			$sql = "UPDATE `Shops` SET `currentnumber` = '$currentnumber' WHERE `shopid` = '$shopid'";
			$mysql->query($sql, $link);
			
			//$sql = "DELETE FROM `Client` WHERE `clientid` = '$clientid'";
			//$mysql->query($sql, $link);
			
			
		}
		//header("location:client.php?id=$id&ShopName=$ShopName&current=$current&PeoNumber=$PeoNumber");
		header("location:client.php?id=$id");
		
		/*
		$temp = 0;
	    $sql = "select all_name from android where id = '$id'";
	    $str = $mysql->query_row($sql, $link);
	    $sql = "SELECT * FROM client where ShopName = '$str[0]'";
	    $result = $mysql->query($sql, $link);
	    
	    
		while ($row = mysql_fetch_row($result)) {
			if ($temp == $del_number) {
				$PeoNumber = $row[3];
				$name = $row[1];
				$CheckTime = $row[5];
				$clientPhone = $row[4];
				//$sql = "insert into client_out (ShopName, clientName, PeoNumber, clientPhone, CheckTime) values ('$row[0]', '$row[1]', '$row[2]', '$row[3]', '$row[4]')";
				$sql = "insert into client_out (ShopName, clientName, PeoNumber, clientPhone, CheckTime, imsi) values ('$row[0]', '$row[1]', '$row[2]', '$row[3]', '$row[4]', '$row[5]')";
				$mysql->query($sql, $link);
				$sql = "select current from android where all_name = '$name'";
				$row = $mysql->query_row($sql, $link);
				$current = $row[0];
				$current = $current - $PeoNumber;
				$sql = "update android set current = '$current' where all_name = '$name'";
				$mysql->query($sql, $link);
				$sql = "delete from client where ShopName = '$str[0]' and clientPhone = '$clientPhone'";
				$mysql->query($sql, $link);
				break;
			}
			$temp++;
		}
		*/
		
	
?>
